package com.education.module.system.sysMenu;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.SysMenu;
import com.education.domain.User;
import com.education.framework.base.BaseServices;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;

@Service
public class SysMenuServices extends BaseServices implements IDao<SysMenu>{

	@Override
	public List<SysMenu> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,menu_name,link_url,remark,pid,icon,create_time,update_time,create_user,update_user,order_index FROM sys_menu");
		
		StringBuffer sqlPage = new StringBuffer();
		sqlPage.append("SELECT count(id) as cNum FROM sys_menu");
		
		String lp = " where ";
		List<Object> argsList = new ArrayList<Object>();
		if(null != searchParams){
			if(null != searchParams.get("menuName") && !"".equals((String)searchParams.get("menuName"))){
				sql.append(lp).append(" menu_name like ? ");
				sqlPage.append(lp).append(" menu_name like ? ");
				argsList.add("%" + searchParams.get("menuName") + "%");
				lp = " and ";
			}
		}
		Object[] args = argsList.toArray();
		List<SysMenu> list = dao.query(pageSQL(sql.toString(),page),args,new SysMenuRowmapper());
		page.setTotalItem(findRecordCount(sqlPage.toString(),args));
		return list;
	}
	
	public List<SysMenu> find(User loginUser) {
		//权限查询逻辑占时未做，所有菜单全部读取
		
		//数据分级处理
		List<SysMenu> list = find();
		
		List<SysMenu> retList = new ArrayList<SysMenu>();
		for(SysMenu menu : list){
			if(-1 == menu.getPid()){
				retList.add(menu);
				recursive(list,menu);
			}
		}
		
		return retList;
	}
	
	
	private void recursive(List<SysMenu> list, SysMenu mParam) {
		for(SysMenu menu : list){
			if(mParam.getId() == menu.getPid()){
				if(null == mParam.getChildList()){
					mParam.setChildList(new ArrayList<SysMenu>());
				}
				mParam.getChildList().add(menu);
				recursive(list,menu);
			}
		}
	}

	public List<SysMenu> find() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,menu_name,link_url,remark,pid,icon,create_time,update_time,create_user,update_user,order_index FROM sys_menu ");
		sql.append("order by order_index");
		List<SysMenu> list = dao.query(sql.toString(),new SysMenuRowmapper());
		return list;
	}

	@Override
	public int save(SysMenu obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("insert into sys_menu ( "); 
 sql.append("id,menu_name,link_url,remark,pid "); 
 sql.append(",icon,create_time,create_user,update_user,order_index ");  
 sql.append(" ) values(?,?,?,?,?,?,?,?,?,?) "); 
 Object[] args = {obj.getId(),obj.getMenuName(),obj.getLinkUrl(),obj.getRemark(),obj.getPid(),obj.getIcon() 
 ,obj.getCreateTime(),obj.getCreateUser().getId(),obj.getUpdateUser().getId(),obj.getOrderIndex() };
		 
		 dao.update(sql.toString(), args);
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public SysMenu findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,menu_name,link_url,remark,pid,icon,create_time,update_time,create_user,update_user,order_index FROM sys_menu ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new SysMenuRowmapper());
	}

	@Override
	public void update(SysMenu obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("update sys_menu "); 
 sql.append("set  "); 
 sql.append("menu_name=?,link_url=?,remark=?,pid=?,icon=? "); 
 sql.append(",update_user=?,order_index=? where id=?  "); 
 Object[] args = {obj.getMenuName(),obj.getLinkUrl(),obj.getRemark(),obj.getPid(),obj.getIcon(),obj.getUpdateUser().getId() 
 ,obj.getOrderIndex(),obj.getId() };
		 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from sys_menu where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class SysMenuRowmapper implements RowMapper<SysMenu> {
		@Override
		public SysMenu mapRow(ResultSet rs, int arg1) throws SQLException {
			SysMenu obj = new SysMenu();
			obj.setId(rs.getInt("id")); 
			obj.setUpdateUserId(rs.getString("update_user")); 
			obj.setIcon(rs.getString("icon")); 
			obj.setUpdateTime(rs.getString("update_time")); 
			obj.setRemark(rs.getString("remark")); 
			obj.setLinkUrl(rs.getString("link_url")); 
			obj.setCreateTime(rs.getString("create_time")); 
			obj.setMenuName(rs.getString("menu_name")); 
			obj.setCreateUserId(rs.getString("create_user")); 
			obj.setPid(rs.getInt("pid")); 
			obj.setOrderIndex(rs.getInt("order_index")); 
			return obj;
		}
	}

}
